//
//  CollectionVC.m
//  BeanTrip
//
//  Created by houlei on 15/7/13.
//  Copyright (c) 2015年 PandaGroup. All rights reserved.
//

#import "CollectionVC.h"
#import "CollectionTableViewCell.h"
#import "CollectionWebVC.h"
#import "DataBaseHandle.h"
#import "CollectionModel.h"

@interface CollectionVC ()

@end

@implementation CollectionVC

- (void)dealloc
{
    [_myTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"特价收藏";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    [[DataBaseHandle shareDataBase] OpenDB];
    return [[[DataBaseHandle shareDataBase] selectAll] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    CollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[CollectionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    [[DataBaseHandle shareDataBase] OpenDB];
    cell.collectionM = [[[DataBaseHandle shareDataBase] selectAll] objectAtIndex:indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionWebVC *collectionWeb = [[CollectionWebVC alloc] init];
    collectionWeb.collectionM = [[[DataBaseHandle shareDataBase] selectAll] objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:collectionWeb animated:YES];
    [collectionWeb release];
}
//添加cell的删除方法
//右侧编辑按钮触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.myTableView setEditing:editing animated:animated];
}
//询问那些行需要编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//设置编辑类型,删除,添加
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
//实现编辑逻辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除数据库里表的内容
    [[DataBaseHandle shareDataBase] OpenDB];
    CollectionModel *collection = [[[DataBaseHandle shareDataBase] selectAll] objectAtIndex:indexPath.row];
    [[DataBaseHandle shareDataBase] deleteCollection:collection.number];
    
    //删除cell
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.myTableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
